<template>
  <view>
    <view class="top-view">
      <!-- 导航栏可根据需要启用 -->
      <!-- <uni-nav-bar
        title="提现"
        backgroundColor="transparent"
        :shadow="false"
        :border="false"
        :statusBar="true"
        leftIcon="back"
        @clickLeft="navBack"
        color="black">
      </uni-nav-bar> -->
    </view>
    <view class="container">
      <view class="info-row">
        <view class="title">提现至微信零钱账户：</view>
        <image
          src="../../static/logo.png"
          class="avatar"
          alt="微信头像"></image>
        <view class="name">{{ hospitalName || "未绑定账户" }}</view>
      </view>

      <view class="bottom-card">
        <view class="title">提现金额</view>
        <view class="money-view">
          <view class="unit">¥</view>
          <view class="num">{{ walletIncome }}</view>
        </view>
        <view class="div-line" />
        <view class="submit-btn" @click="handleConfirm">确定</view>
      </view>
    </view>
  </view>
</template>

<script>
import * as nurseInfo from "../../common/api/nurseInfo.js";
export default {
  data() {
    return {
      // 从钱包页面传递的账户余额
      walletIncome: 0,
      account: "", // 护士姓名
      bankId: "", // 银行卡ID
      hospitalId: "", // 护士ID
      expenditure: "", // 提现金额
      hospitalName: "", // 银行账户名称
      listLoading: false, // 修复：补充列表加载状态变量
    };
  },
  onLoad(options) {
    // 接收从钱包页面传递的余额参数
    const receivedIncome = decodeURIComponent(options.income || "0");
    this.walletIncome = Number(receivedIncome);
    console.log("从钱包页面接收的账户余额：", this.walletIncome);

    // 加载用户及银行卡信息
    this.getList();
  },
  methods: {
    // 获取用户及银行卡信息
    getList() {
      this.listLoading = true;
      const dataJson = uni.getStorageSync("user_info") || "{}";
      const data = JSON.parse(dataJson) || {};
      console.log("用户信息数据：", data);

      // 赋值用户基本信息
      this.hospitalId = data.nurseUid;
      this.account = data.nurseName || "未知用户";

      // 请求银行卡信息
      const param = { uid: data.nurseUid };
      nurseInfo
        .queryByBankId(param)
        .then((result) => {
          console.log("银行卡列表数据：", result);
          if (result.data.data && result.data.data.length > 0) {
            this.bankId = result.data.data[0].id;
            this.hospitalName = result.data.data[0].hospitalCompany;
          } else {
            uni.showToast({ title: "暂无绑定的银行卡", icon: "none" });
          }
        })
        .catch((error) => {
          console.error("获取银行卡失败：", error);
          uni.showToast({ title: "加载银行卡失败", icon: "none" });
        })
        .finally(() => {
          this.listLoading = false;
        });
    },

    // 处理提现确认
    handleConfirm() {
      // 基础校验
      if (!this.bankId) {
        uni.showToast({ title: "请先绑定银行卡", icon: "none" });
        return;
      }
      if (this.walletIncome <= 0) {
        uni.showToast({ title: "提现金额必须大于0", icon: "none" });
        return;
      }

      // 构建请求参数（修复hospitalId赋值错误）
      const param = {
        account: this.account,
        bankId: this.bankId,
        hospitalId: this.hospitalId, // 已修正为正确的护士ID
        expenditure: this.walletIncome,
      };
      console.log("提现请求参数：", param);

      // 调用提现接口
      nurseInfo
        .addNurseFinance(param)
        .then((result) => {
          console.log("提现接口返回结果：", result);
          // 根据实际接口成功标识调整（此处假设code=200为成功）
          if (result.data.code === 200 || result.data.success) {
            uni.showToast({
              title: "提现申请提交成功",
              icon: "success",
              duration: 1500,
              success: () => {
                // 延迟跳转确保用户看到提示
                setTimeout(() => {
                  // 跳转到"我的"页面（请替换为实际路由）
                  uni.switchTab({
                    url: "/pages/mine/mine", // 核心：替换为你的"我的"页面路由
                    fail: () => {
                      // 非Tab页降级方案
                      uni.redirectTo({
                        url: "/pages/mine/mine",
                        fail: () => {
                          uni.showToast({
                            title: "跳转失败，请手动返回",
                            icon: "none",
                          });
                        },
                      });
                    },
                  });
                }, 1500);
              },
            });
          } else {
            // 业务逻辑失败
            uni.showToast({
              title: result.data.msg || "提现失败，请重试",
              icon: "none",
              duration: 1500,
            });
          }
        })
        .catch((error) => {
          // 接口调用异常
          console.error("提现接口调用失败：", error);
          uni.showToast({ title: "网络异常，提现失败", icon: "none" });
        });
    },

    // 返回上一页
    navBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
page {
  background: white;
}
</style>

<style lang="scss" scoped>
.top-view {
  height: 700rpx;
  background: linear-gradient(
    to bottom,
    rgba(207, 234, 238, 1),
    rgba(207, 234, 238, 0)
  );
}

.container {
  margin-top: -660rpx;
  display: flex;
  flex-direction: column;
}

.info-row {
  display: flex;
  align-items: center;
  padding: 36rpx;
  box-sizing: border-box;

  .title {
    color: #232323;
    font-size: 30rpx;
    flex: 1;
  }

  .avatar {
    width: 60rpx;
    height: 60rpx;
    border-radius: 50%;
  }

  .name {
    color: #232323;
    font-size: 34rpx;
    margin-left: 10rpx;
  }
}

.bottom-card {
  min-height: 536rpx;
  border-top-left-radius: 32rpx;
  border-top-right-radius: 32rpx;
  background: white;
  padding: 30rpx;
  box-sizing: border-box;

  .title {
    color: #000000;
    font-size: 30rpx;
    margin-left: 20rpx;
  }

  .money-view {
    display: flex;
    color: #232323;
    margin-top: 26rpx;
    margin-left: 20rpx;

    .unit {
      font-size: 64rpx;
      font-weight: 700;
      line-height: 76rpx;
    }

    .num {
      font-size: 72rpx;
      font-weight: 600;
      line-height: 84rpx;
      margin-left: 24rpx;
    }
  }

  .div-line {
    background: #232323;
    width: calc(100% - 20rpx);
    height: 2rpx;
    margin-top: 22rpx;
    margin-left: 20rpx;
  }

  .submit-btn {
    background: #2eb0d9;
    height: 80rpx;
    border-radius: 40rpx;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30rpx;
    margin-top: 48rpx;
    cursor: pointer;
  }

  .submit-btn:active {
    opacity: 0.9;
  }
}
</style>
